package com.market.service.wx.impl;

import com.market.bean.MarketCategory;
import com.market.bean.MarketCategoryExample;
import com.market.bean.MarketGoods;
import com.market.bean.MarketGoodsExample;
import com.market.bean.vo.wx.CurrentCategoryVo;
import com.market.bean.vo.wx.IndexCategoryVo;
import com.market.mapper.MarketCategoryMapper;
import com.market.mapper.MarketGoodsMapper;
import com.market.service.wx.WxCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wh194@foxmail.com
 * @since 2023/01/02 17:01
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;


    /**
     * 当前类目
     *
     * @param id
     * @return
     */
    @Override
    public CurrentCategoryVo current(Integer id) {
        CurrentCategoryVo currentCategoryVo = new CurrentCategoryVo();

        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);

        currentCategoryVo.setCurrentCategory(marketCategory);


        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        marketCategoryExample1.createCriteria().andDeletedEqualTo(false).andPidEqualTo(id);
        List<MarketCategory>  currentSubCategory = marketCategoryMapper.selectByExample(marketCategoryExample1);

        currentCategoryVo.setCurrentSubCategory(currentSubCategory);

        return currentCategoryVo;
    }

    /**
     * 类目索引
     *
     * @return
     */
    @Override
    public IndexCategoryVo index() {
        IndexCategoryVo indexCategoryVo = new IndexCategoryVo();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andDeletedEqualTo(false).andPidEqualTo(0);

        List<MarketCategory>  marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        indexCategoryVo.setCategoryList(marketCategories);

        MarketCategory currentCategory = marketCategories.get(0);
        indexCategoryVo.setCurrentCategory(currentCategory);

        Integer pid = currentCategory.getPid();

        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        marketCategoryExample1.createCriteria().andDeletedEqualTo(false).andPidEqualTo(pid);
        List<MarketCategory>  currentSubCategory = marketCategoryMapper.selectByExample(marketCategoryExample1);


        indexCategoryVo.setCurrentSubCategory(currentSubCategory);

        return indexCategoryVo;
    }

}
